﻿namespace GoodAdmin.Core;

public class DbRepository<T> : SimpleClient<T> where T : class, new()
{
    public DbRepository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
    {
        Context = App.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScopeWithAttr<T>();
    }


    /// <summary>
    /// 获取指定表的单个字段
    /// </summary>
    /// <param name="WhereExpression">查询条件</param>
    /// <param name="SelectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<TResult> GetFieldFirstAsync<TResult>(Expression<Func<T, bool>> WhereExpression, Expression<Func<T, TResult>> SelectExpression)
    {
        return Context.Queryable<T>().Where(WhereExpression).Select(SelectExpression).FirstAsync();
    }

    /// <summary>
    /// 获取指定字段值的列表
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="WhereExpression">查询条件</param>
    /// <param name="SelectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<List<TResult>> GetFieldListAsync<TResult>(Expression<Func<T, bool>> WhereExpression, Expression<Func<T, TResult>> SelectExpression)
    {
        return Context.Queryable<T>().Where(WhereExpression).Select(SelectExpression).ToListAsync();
    }

    /// <summary>
    /// 查询指定表数据，并转换为指定对象
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <returns></returns>
    public virtual Task<TResult> GetFirstAsync<TResult>(Expression<Func<T, bool>> whereExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).Select<TResult>().FirstAsync();
    }

    /// <summary>
    /// 查询指定表列表数据，并转换为指定对象
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <returns></returns>
    public virtual Task<List<TResult>> GetListAsync<TResult>()
    {
        return Context.Queryable<T>().Select<TResult>().ToListAsync();
    }

    public virtual Task<List<TResult>> GetOrderListAsync<TResult>(Expression<Func<T, object>> expression, OrderByType type = OrderByType.Asc)
    {
        return Context.Queryable<T>().OrderBy(expression, type).Select<TResult>().ToListAsync();
    }

    /// <summary>
    /// 获取指定字段值的列表（当前service查询其它实体信息时使用）
    /// </summary>
    /// <typeparam name="TEntity">需要查询实体</typeparam>
    /// <typeparam name="TResult">返回值类型</typeparam>
    /// <param name="WhereExpression">查询条件</param>
    /// <param name="SelectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<List<TResult>> GetFieldListAsync<TEntity,TResult>(Expression<Func<TEntity, bool>> WhereExpression, Expression<Func<TEntity, TResult>> SelectExpression)
    {
        return Context.Queryable<TEntity>().Where(WhereExpression).Select(SelectExpression).ToListAsync();
    }

    /// <summary>
    /// 获取下级
    /// </summary>   
    /// <param name="parentIdExpression"></param>
    /// <param name="primaryKeyValue"></param>
    /// <param name="isContainOneself"></param>
    /// <returns></returns>
    public virtual Task<List<T>> GetChildListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue, bool isContainOneself = true)
    {
        return Context.Queryable<T>().ToChildListAsync(parentIdExpression, primaryKeyValue, isContainOneself);
    }
}